﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.EmpbasicService;
using EHR.Common;
using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;

namespace CommunitySystem.PageView
{
    public partial class SelOrgSelEmpNameWindow : ChildWindow
    {
        private EmpBasicServiceClient Client = new EmpBasicServiceClient();
        public ObservableCollection<Emp_BasicInfoModel> SelectItems1 = new ObservableCollection<Emp_BasicInfoModel>();

        public SelOrgSelEmpNameWindow(ObservableCollection<Emp_BasicInfoModel> _SelectItems)
        {
            InitializeComponent();

            SelectItems1 = _SelectItems;
            foreach (var item in _SelectItems)
            {
                SelectItems.Add(new Emp_BasicInfoModel() { ID = item.ID, Name = item.Name });
            }

            Client.GetEmp_BasicInfoByConCompleted += new EventHandler<GetEmp_BasicInfoByConCompletedEventArgs>(Client_GetEmp_BasicInfoByConCompleted);

            Client.GetDengLuJueSeCompleted += new EventHandler<EmpbasicService.GetDengLuJueSeCompletedEventArgs>(Client_GetDengLuJueSeCompleted);
            Client.GetDengLuJueSeAsync(Session.LoginUser.LoginName);

            Client.GetOrgForHRModelbyIDsCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs>(Client_GetOrgForHRModelbyIDsCompleted);
            Client.GetOrgForHRModelCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelCompletedEventArgs>(Client_GetOrgForHRModelCompleted);
            Client.GetDengLuOrgCompleted += new EventHandler<EmpbasicService.GetDengLuOrgCompletedEventArgs>(Client_GetDengLuOrgCompleted);
        }
        private bool isrc { get; set; }
        void Client_GetEmp_BasicInfoByConCompleted(object sender, GetEmp_BasicInfoByConCompletedEventArgs e)
        {
            if (isrc) return;
            if (e.Result.Count > 0)
            {
                this.sprclb.Background = new SolidColorBrush(Colors.White);
                foreach (var item in e.Result)
                {
                    StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, Background = new SolidColorBrush(Colors.White) };
                    sp.Cursor = Cursors.Hand;
                    Image img = new Image() { Height = 25, Width = 25 };
                    BitmapImage bi = new BitmapImage(new Uri("/CommunitySystem;component/Images/UI/photo.png", UriKind.RelativeOrAbsolute));
                    img.Source = bi;
                    sp.Children.Add(img);
                    TextBlock cb_gjj = new TextBlock();
                    cb_gjj.Width = 60;
                    cb_gjj.Margin = new Thickness(5, 2, 2, 2);
                
                    sp.MouseLeftButtonDown += new MouseButtonEventHandler(cb_gjj_MouseLeftButtonDown);
                    cb_gjj.Text = item.Name;
                    sp.Tag = item;
                    sp.Children.Add(cb_gjj);
                    this.sprclb.Children.Add(sp);
                }
                isrc = true;
            }
        }

        public ObservableCollection<Emp_BasicInfoModel> SelectItems = new ObservableCollection<Emp_BasicInfoModel>();
        void cb_gjj_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            
            var conmo = ((StackPanel)sender).Tag as Emp_BasicInfoModel;
            foreach (StackPanel item in sprclb.Children)
            {
                if (SelectItems.Where(p => p.ID != conmo.ID).Count() > 0)
                {
                    item.Background = new SolidColorBrush(Colors.White);
                }
                ((StackPanel)sender).Background = new SolidColorBrush(Colors.DarkGray);
            }
            SelectItems.Clear();
          
            if (SelectItems.Where(p => p.ID == conmo.ID).ToList().Count == 0)
                SelectItems.Add(conmo);
        }
        void Client_GetDengLuJueSeCompleted(object sender, EmpbasicService.GetDengLuJueSeCompletedEventArgs e)
        {
            if (e.Result != "人事干部")
            {
                if (TreeOfLife.ItemsSource != null) TreeOfLife.ItemsSource = null;
                TreeOfLife.Visibility = System.Windows.Visibility.Visible;
                ListBoxOfLife.Visibility = System.Windows.Visibility.Collapsed;
                Client.GetOrgForHRModelAsync(0);

            }
            else
            {
                TreeOfLife.Visibility = System.Windows.Visibility.Collapsed;
                ListBoxOfLife.Visibility = System.Windows.Visibility.Visible;
                Client.GetDengLuOrgAsync(Session.LoginUser.LoginName);
              
            }
        }
        void Client_GetDengLuOrgCompleted(object sender, EmpbasicService.GetDengLuOrgCompletedEventArgs e)
        {
            Client.GetOrgForHRModelbyIDsAsync("(" + e.Result + ")");
        }
        public int orgid { get; set; }
        public List<string> orgids = new List<string>();
        void Client_GetOrgForHRModelbyIDsCompleted(object sender, EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs e)
        {
            ListBoxOfLife.ItemsSource = e.Result;
            ListBoxOfLife.SelectedIndex = 0;
            orgid = Convert.ToInt32(e.Result[0].ID);
           
            orgids.Clear();
            orgids.Add(orgid.ToString());
          
        }
        public ObservableCollection<OrgForHRModel> OrgList =
           new ObservableCollection<OrgForHRModel>();
        ObservableCollection<EmpbasicService.OrgForHRModel> forumList = new ObservableCollection<EmpbasicService.OrgForHRModel>();

        void Client_GetOrgForHRModelCompleted(object sender, EmpbasicService.GetOrgForHRModelCompletedEventArgs e)
        {
            OrgList = e.Result;
            forumList.Clear();
            foreach (EmpbasicService.OrgForHRModel i in e.Result)
            {
                forumList.Add(i);
            }
            AddTreeNode("全部单位", null);
        }
        private void AddTreeNode(string type, TreeViewItem treeViewItem)
        {
            List<EmpbasicService.OrgForHRModel> result = (from forumInfo in forumList
                                                          where forumInfo.Type == type
                                                          select forumInfo).ToList<EmpbasicService.OrgForHRModel>();

            if (result.Count > 0)
            {
                foreach (EmpbasicService.OrgForHRModel foruminfo in result)
                {
                    TreeViewItem objTreeNode = new TreeViewItem();
                    //CheckBox cb = new CheckBox();
                    //cb.Content = foruminfo.Name;
                    TextBlock cb = new TextBlock();
                    cb.Text = foruminfo.Name;
                    cb.Tag = objTreeNode;
                    cb.DataContext = foruminfo;
                
                    //cb.Click += new RoutedEventHandler(cb_Checked);
                    cb.MouseLeftButtonDown += new MouseButtonEventHandler(cb_MouseLeftButtonDown);
                    objTreeNode.Header = cb;
                 
                    //if ((objTreeNode.Header as CheckBox).Content.ToString() == "组织机构") objTreeNode.IsExpanded = true;
                    if ((objTreeNode.Header as TextBlock).Text.ToString() == "组织机构") objTreeNode.IsExpanded = true;
                  
                    //添加根节点
                    if (treeViewItem == null)
                    {
                        TreeOfLife.Items.Add(objTreeNode);
                    }
                    else
                    {
                        treeViewItem.Items.Add(objTreeNode);
                    }
                    AddTreeNode(foruminfo.Name, objTreeNode);
                }
            }
        }
        private int OrgID = 0;
        void cb_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            EmpbasicService.OrgForHRModel rm = cb.DataContext as EmpbasicService.OrgForHRModel;
            if (cb.IsChecked == true)
            {
             
                if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                    orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                string ss = "";
                foreach (string i in orgids)
                    ss += i + ",";
                if (string.IsNullOrEmpty(ss) || orgids.Contains("79")) ss = "0";
                else ss = ss.Substring(0, ss.Length - 1);
          
              
                CheckChildItem(cb.Tag as TreeViewItem);
            }
            else
            {
             
                if (orgids != null)
                {
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                }
                string ss = "";
                foreach (string i in orgids)
                    ss += i + ",";
                if (ss.Length > 1) ss = ss.Substring(0, ss.Length - 1);
            
                UnCheckChildItem(cb.Tag as TreeViewItem);
            }
        }
        void cb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            isrc = false;
        
            TextBlock cb = sender as TextBlock;
            EmpbasicService.OrgForHRModel rm = cb.DataContext as EmpbasicService.OrgForHRModel;

            this.sprclb.Children.Clear();
            Client.GetEmp_BasicInfoByConAsync(string.Format(" and orgid={0}",rm.ID));
        }

       
        void CheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = true;
                    if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    CheckChildItem(tvi);
                }
            }
        }
        void UnCheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = false;
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    UnCheckChildItem(tvi);
                }
            }
        }
        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            isrc = false;
        
            this.sprclb.Children.Clear();
            orgid = Convert.ToInt32(((OrgForHRModel)(ListBoxOfLife.SelectedItem)).ID);

            Client.GetEmp_BasicInfoByConAsync(string.Format(" and orgid={0}", orgid));
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

